package com.sgb.kjwl.viewmodel.self.publish;

import androidx.annotation.RequiresApi;
import androidx.databinding.ObservableField;

import android.os.Build;

import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.NewGoodsDetailEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.publish.GoodsDetailActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GoodsDetailModel extends BaseViewModel {
    public ObservableField<NewGoodsDetailEntity> newGoodsDetailEntity = new ObservableField<>(new NewGoodsDetailEntity());
    private GoodsDetailActivity activity;
    private SelfRepertory repertory;
    private int page = 1;
    private int size = 20;
    public int total;

    public GoodsDetailModel(GoodsDetailActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }


    public void getGoodsDetail(String id) {
        DialogHelper.showProgressDialog2(activity, null, "数据加载中...", 0, false, null, false);
        NewAPIManager.getInstance().getGoodsAPI().goodsDetail(id).enqueue(new ICallBack<BaseEntity<NewGoodsDetailEntity>>() {

            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(BaseEntity<NewGoodsDetailEntity> data) {
                newGoodsDetailEntity.set(data.getData());
                activity.initData();
            }
        });
    }

    public String getNo() {
        return repertory.getNo();
    }

    //上下架删除商品
    public void upDowngetGoodsModel(String opt, List<String> goodsIds) {
        Map map = new HashMap();
        map.put("status", opt);
        map.put("goodsIds", goodsIds);
        NewAPIManager.getInstance().getGoodsReleasedAPI().upDown(map).enqueue(new ICallBack<BaseEntity<Object>>() {
            @Override
            public void onSuccess(BaseEntity<Object> data) {
                activity.optSuccess(opt);
            }
        });
    }

    //商品详情改价
    public void modifyGoodsDetailPriceModel(String id, String price) {
        NewAPIManager.getInstance().getGoodsReleasedAPI().modifyGoodsDetailPrice(id, price).enqueue(new ICallBack<BaseEntity<Object>>() {
            @Override
            public void onSuccess(BaseEntity<Object> data) {
                activity.modifySuccess();
            }
        });
    }

}
